﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.ComponentModel;
using System.Data;
using System.Data.Odbc;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace Anata
{
    /// <summary>
    /// Configure database connection string
    /// </summary>
    public partial class DatabaseConfigurationForm : Form
    {
        AdminDialogueForm _f;

        /// <summary>
        /// Construktor
        /// </summary>
        public DatabaseConfigurationForm() { }

        /// <summary>
        /// Construktor with connection to main form
        /// </summary>
        /// <param name="f">AdminDialogueForm</param>
        public DatabaseConfigurationForm(AdminDialogueForm f)
        {
            _f = f;
            InitializeComponent();
        }

        /// <summary>
        /// Carry on change connection string function
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("Pastikan data yang anda masukkan sudah benar, sistem tidak dapat melakukan validasi secara langsung.", "Konfirmasi", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                    config.ConnectionStrings.ConnectionStrings.Clear();
                    config.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings("Anata.Properties.Settings.Anata2ConnectionString",
                        String.Format("Dsn=PostgreSQL30;database={0};server={1};port=5432;uid={2};pwd={3};sslmode=disable;readonly=0;protocol=7.4;fakeoidindex=0;showoidcolumn=0;rowversioning=0;showsystemtables=0;fetch=100;socket=4096;unknownsizes=0;maxvarcharsize=255;maxlongvarcharsize=8190;debug=0;commlog=0;optimizer=0;ksqo=1;usedeclarefetch=0;textaslongvarchar=1;unknownsaslongvarchar=0;boolsaschar=1;parse=0;cancelasfreestmt=0;extrasystableprefixes=dd_;lfconversion=1;updatablecursors=1;disallowpremature=0;trueisminus1=0;bi=0;byteaaslongvarbinary=0;useserversideprepare=0;lowercaseidentifier=0;gssauthusegss=0;xaopt=1",
                        textBox2.Text, textBox1.Text, textBox3.Text, textBox4.Text)));
                    config.Save(ConfigurationSaveMode.Modified, true);
                    ConfigurationManager.RefreshSection("connectionStrings");
                    Properties.Settings.Default.Reload();
                    _f.Regenerate();
                    this.Close();                    
                }
            }
            catch (Exception msg)
            {
                MessageBox.Show(msg+"Koneksi tidak berhasil.");
            }

            //Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            //config.ConnectionStrings.ConnectionStrings.Clear();
            //config.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings(
            //   "Anata.Properties.Settings.anatadbConnectionString",
            //   "Dsn=PostgreSQL30;database=anatadb;server=localhost;port=5432;uid=postgres;pwd=root;sslmode=disable;readonly=0;protocol=7.4;fakeoidindex=0;showoidcolumn=0;rowversioning=0;showsystemtables=0;fetch=100;socket=4096;unknownsizes=0;maxvarcharsize=255;maxlongvarcharsize=8190;debug=0;commlog=0;optimizer=0;ksqo=1;usedeclarefetch=0;textaslongvarchar=1;unknownsaslongvarchar=0;boolsaschar=1;parse=0;cancelasfreestmt=0;extrasystableprefixes=dd_;lfconversion=1;updatablecursors=1;disallowpremature=0;trueisminus1=0;bi=0;byteaaslongvarbinary=0;useserversideprepare=0;lowercaseidentifier=0;gssauthusegss=0;xaopt=1"));
            //config.Save(ConfigurationSaveMode.Modified, true);
            //ConfigurationManager.RefreshSection("connectionStrings");
            //MessageBox.Show(ConfigurationManager.ConnectionStrings["Anata.Properties.Settings.anatadbConnectionString"].ConnectionString);
        }

        /// <summary>
        /// Function for changing app.config
        /// </summary>
        /// <param name="KeyName">Key Name</param>
        /// <param name="KeyValue">Key Value</param>
        private void changeValue(String KeyName, String KeyValue)
        {
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.AppSettings.Settings[KeyName].Value = KeyValue;
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }

        /// <summary>
        /// Function to show app.config value
        /// </summary>
        private void ShowConfig()
        {
            foreach (string key in ConfigurationManager.AppSettings)
                MessageBox.Show(key + " : " + ConfigurationManager.AppSettings[key]);
        }

        /// <summary>
        /// Cancel button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
    }
}
